% Mengzi Zhang
% 16 Nov 2011
% CIS 520 Project

% Unsupervised - kernel PCA
%   Uses Spider library
%   Assume data .mat file is loaded before calling this script

% Can't really check errors, since that notion of Y lbls doesn't exist in
% unsupervised training. Cluster 1 could be star 4, cluster 4 could be star 2,
% there's no way to compare the numbers to check error.


%% CV training / test sets

% Xtrain = make_sparse(train(bsxfun(@gt, [train().category], 6)));
% Ytrain = double([train(bsxfun(@gt, [train().category], 6)).rating])';
% 
% XminiTest = make_sparse(train(bsxfun(@lt, [train().category], 7)));
% YminiTest = double([train(bsxfun(@lt, [train().category], 7)).rating])';


% run on a smaller subset to see if still get out of memory
XcvTrain = make_sparse(train(bsxfun(@eq, [train().category], 9)));
YcvTrain = double([train(bsxfun(@eq, [train().category], 9)).rating])';

XcvTest = make_sparse(train(bsxfun(@eq, [train().category], 3)));
YcvTest = double([train(bsxfun(@eq, [train().category], 3)).rating])';



%% Train

% Set up kpca obj
kpca_obj = kpca;
kpca_obj.feat = 2;
% Set kernel to use. Default is linear.
%kpca_obj.child = 'linear';

% Train
% No output Y data for clustering. Create with one var
data_train = data (XcvTrain);
% Use feval, because supplied data has a var named train. Using train() will
%   point to that var, instead of the Spider fn.
[predictions_tr model] = feval ('train', kpca_obj, data_train);


%% Plot

% See the predictions
predictions_tr.X
% cluster centers
%model.mu
% cluster assignment of each training sample
%model.y

% idx for examples with predicted label 1
% idx = find (predictions_tr.X == 1);
% clf;
% hold on;
% plot (data_train.X(idx, 1), data_train.X(idx, 2), 'r.');
% 
% % idx for examples with predicted label 2
% idx = find (predictions_tr.X == 2);
% plot (data_train.X(idx, 1), data_train.X(idx, 2), 'b.');
% 
% idx = find (predictions_tr.X == 3);
% plot (data_train.X(idx, 1), data_train.X(idx, 2), 'g.');


%% Test

% Test
data_test = data (XcvTest);
predictions_ts = feval ('test', model, data_test);

% See the predictions
predictions_ts.X


